package com.reds.backend.service.impl.admin;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reds.backend.pojo.Admin;
import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reds.backend.service.admin.adminCheckService;
import com.reds.backend.mapper.adminMapper;

import java.util.List;

@Service
public class adminCheckServiceImpl implements adminCheckService{
    @Autowired
    adminMapper adminMapper;
    @Override
    public JSONObject getAllAdmin(int sizes, int page,String role) {
        System.out.println(sizes);
        System.out.println(page);
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        System.out.println(role);
        if(role.equals("所有"))
            queryWrapper.orderByDesc("id");
        else
            queryWrapper.orderByDesc("id").eq("role",role);

        IPage<Admin> iPage = new Page<>(page,sizes);
        List<Admin> list = adminMapper.selectPage(iPage,queryWrapper).getRecords();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("admin",list);
        jsonObject.put("length",adminMapper.selectCount(queryWrapper));
        return jsonObject;
    }

    @Override
    public String changeAdminRole(int id, String role) {
        Admin admin = adminMapper.selectById(id);
        if(admin.getId() == StpAdminUtil.getLoginIdAsInt())
            return "不能修改自己的角色";
        else admin.setRole(role);
        adminMapper.updateById(admin);
        return "success";
    }
}
